package com.ws.service.impl;

import com.github.pagehelper.Page;
import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.Department;
import com.ws.repository.DepartmentRepository;
import com.ws.service.DepartmentService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/23 23:30
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;

    @Override
    public PaasPage<Department> getDepartmentPage(Pageable pageable, String departmentName,String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        Department department = new Department();
        department.setDepartmentName(departmentName);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        department.setStatusCd(statusCd);
        Page<Department> departmentPage = departmentRepository.getDepartmentPage(department);
        return PageUtil.transformPage(departmentPage);
    }

    @Override
    public Department saveDepartment(Department department) {
        if(null == department.getDepartmentId()){
            department.setCreateTime(new Date());
            department.setStatusCd(CommonConstant.ADD_STATUS_CD);
            departmentRepository.saveDepartment(department);
        }else {
            departmentRepository.updateDepartment(department);
        }

        return department;
    }

    @Override
    public Department getDepartmentBasicInfo(Long departmentId) {
        return departmentRepository.getDepartmentBasicInfo(departmentId);
    }

    @Override
    public void delDepartment(Long departmentId) {
        departmentRepository.delDepartment(departmentId);
    }

    @Override
    public Department updateDepartment(Long departmentId, Department department) {
        departmentRepository.updateDepartment(department);
        return department;
    }

    @Override
    public List<Department> getDepartmentList(String departmentName, String statusCd) {
        Department department = new Department();
        department.setDepartmentName(departmentName);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        department.setStatusCd(statusCd);
        List<Department> departmentList= departmentRepository.getDepartmentList(department);
        return departmentList;
    }
}
